//
//  GJFileManager.m
//  RobotSDK
//
//  Created by wbb on 2022/9/19.
//

#import "GJFileManager.h"

@implementation GJFileManager
+ (BOOL)createDir:(NSString *)path {
    NSAssert(path > 0, @"%s: The length of path should be greater than 0.", __FUNCTION__);
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isSuccess = YES;
    // 判断目录是否已经存在
    if (![fileManager fileExistsAtPath:path]) {
        // 创建文件目录
        NSError *error = nil;
        isSuccess = [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
        if (error) {
            NSLog(@"Create directory failed: %@",[error localizedDescription]);
        }
    }
    return isSuccess;
}
+ (BOOL)createFile:(NSString *)filePath {
    NSAssert(filePath > 0, @"%s: The length of filePath should be greater than 0.", __FUNCTION__);
    NSFileManager *fileManager = [NSFileManager defaultManager];
    // 提取出文件存放的目录
    NSString *dir = [filePath stringByDeletingLastPathComponent];
    // 判断目录是否已经存在
    if (![fileManager fileExistsAtPath:dir]) {
        // 如果还没有存在，则需要新建
        NSError *error = nil;
        [fileManager createDirectoryAtPath:dir withIntermediateDirectories:YES attributes:nil error:&error];
        if (error) {
            NSLog(@"Create directory failed: %@",[error localizedDescription]);
            return NO;
        }
    }
    // 创建文件
    return [fileManager createFileAtPath:filePath contents:nil attributes:nil];
}

+ (void)moveItemAtPath:(NSString *)fromPath toPath:(NSString *)toPath  {

    NSFileManager *fileManager = [NSFileManager defaultManager];
    // 执行移动操作
    NSError *error = nil;
    BOOL isSuccess = [fileManager moveItemAtPath:fromPath toPath:toPath error:&error];
    if (isSuccess) {
        NSLog(@"moveFile Success!");
    } else {
        NSLog(@"moveFile Field：%@",[error localizedDescription]);
    }

}

+ (BOOL)deleteFile:(NSString *)filePath {
    NSError *error = nil;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isSuccess = [fileManager removeItemAtPath:filePath error:&error];
    if (error) {
        NSLog(@"deleteFile Field：%@",[error localizedDescription]);
    }
    return isSuccess;
}

+ (NSString *)fromPath {
    return  [NSHomeDirectory() stringByAppendingString:[NSString stringWithFormat:@"/Documents/tmp"]];
}

+ (NSString *)toPath {
    return  [NSHomeDirectory() stringByAppendingString:[NSString stringWithFormat:@"/Documents/gif"]];
}
@end
